//
//  Flipable.swift
//
//  Created by tao ma on 2022/11/4.
//

import UIKit

// MARK: - [WmcFlipable]

protocol WmcFlipable {
    func flip(directionHorizontal: Bool)
}

extension WmcFlipable where Self: WmcCropperViewController {
    func flip(directionHorizontal: Bool = true) {
        let size: CGSize = scrollView.contentSize
        let contentOffset = scrollView.contentOffset
        let bounds: CGSize = scrollView.bounds.size

        scrollView.contentOffset = CGPoint(x: size.width - bounds.width - contentOffset.x, y: contentOffset.y)

        let image = imageView.image
        let fliped: Bool = (image?.imageOrientation == .upMirrored)
        // TODO: multi imageOrientation

        if directionHorizontal {
            flipAngle += -2.0 * totalAngle
        } else {
            flipAngle += CGFloat.pi - 2.0 * totalAngle
        }

        imageView.image = image?.withOrientation(fliped ? .up : .upMirrored)

        scrollView.transform = CGAffineTransform(rotationAngle: totalAngle)
        updateButtons()
    }
}
